﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Notorious_World_Editor
{
    public partial class Tile_Selector : Form
    {
        SGP.ManagedDirect3D d3D = SGP.ManagedDirect3D.Instance;
        SGP.ManagedTextureManager tm = SGP.ManagedTextureManager.Instance;

        public event EventHandler SelectBlock;

        int tileSetHeight;

        public int TileSetHeight
        {
            get { return tileSetHeight; }
            set { tileSetHeight = value; }
        }

        int tileSetWidth;

        public int TileSetWidth
        {
            get { return tileSetWidth; }
            set { tileSetWidth = value; }
        }

        int tileWidth;

        public int TileWidth
        {
            get { return tileWidth; }
            set { tileWidth = value; }
        }

        int tileHeight;

        public int TileHeight
        {
            get { return tileHeight; }
            set { tileHeight = value; }
        }

        Point selectedTile = new Point(0, 0);

        public Point SelectedTile
        {
            get { return selectedTile; }
            set { selectedTile = value; }
        }

        Point blockStart;

        public Point BlockStart
        {
            get { return blockStart; }
            set { blockStart = value; }
        }

        Point blockEnd;

        public Point BlockEnd
        {
            get { return blockEnd; }
            set { blockEnd = value; }
        }

        Rectangle selectedBlock;

        public Rectangle SelectedBlock
        {
            get { return selectedBlock; }
            set { selectedBlock = value; }
        }

        bool looping = true;

        public bool Looping
        {
            get { return looping; }
            set { looping = value; }
        }

        public Tile_Selector()
        {
            InitializeComponent();
            looping = true;
        }

        private void Tile_Selector_FormClosing(object sender, FormClosingEventArgs e)
        {
            looping = false;
        }

        private void splitContainer1_Panel1_MouseMove(object sender, MouseEventArgs e)
        {
            toolStripStatusLabelTileCoord.Text = "Tile Coord: " + ((e.X + hScrollBar1.Value) / tileWidth) + ", " + ((e.Y + vScrollBar1.Value) / tileHeight);
            toolStripStatusLabelTileID.Text = "TileID: " + (((e.X + hScrollBar1.Value) / tileWidth) + (((e.Y + vScrollBar1.Value) / tileHeight) * tileSetWidth));

            if (e.Button == MouseButtons.Left)
            {
                blockEnd = new Point(e.X + hScrollBar1.Value, e.Y + vScrollBar1.Value);

                int wid = blockEnd.X / tileWidth - blockStart.X / tileWidth;
                int hit = blockEnd.Y / tileHeight - blockStart.Y / tileHeight;

                if (wid >= 0 && hit >= 0)
                {
                    selectedBlock = new Rectangle(blockStart.X / tileWidth * tileWidth, blockStart.Y / tileHeight * tileHeight,
                        (wid + 1) * tileWidth, (hit + 1) * tileHeight);

                    if (SelectBlock != null)
                        SelectBlock(this, EventArgs.Empty);
                }
            }
        }

        private void splitContainer1_Panel1_MouseDown(object sender, MouseEventArgs e)
        {
            blockStart = new Point(e.X + hScrollBar1.Value, e.Y + vScrollBar1.Value);
        }

        private void splitContainer1_Panel1_MouseUp(object sender, MouseEventArgs e)
        {
            blockEnd = new Point(e.X + hScrollBar1.Value, e.Y + vScrollBar1.Value);

            int wid = blockEnd.X / tileWidth - blockStart.X / tileWidth;
            int hit = blockEnd.Y / tileHeight - blockStart.Y / tileHeight;

            if (wid >= 0 && hit >= 0)
            {
                selectedBlock = new Rectangle(blockStart.X / tileWidth * tileWidth, blockStart.Y / tileHeight * tileHeight, 
                    (wid + 1) * tileWidth, (hit + 1) * tileHeight);

                if (SelectBlock != null)
                    SelectBlock(this, EventArgs.Empty);
            }
        }

        public void Update()
        {
            DisplayScrollBars();
            SetScrollBarValues();
        }

        private void DisplayScrollBars()
        {
            if (splitContainer1.Panel1.Width > tileSetWidth * tileWidth - vScrollBar1.Width)
            {
                hScrollBar1.Visible = false;
            }
            else
            {
                hScrollBar1.Visible = true;
            }

            if (splitContainer1.Panel1.Height > tileSetHeight * tileHeight - hScrollBar1.Height)
            {
                vScrollBar1.Visible = false;
            }
            else
            {
                vScrollBar1.Visible = true;
            }
        }

        private void SetScrollBarValues()
        {
            vScrollBar1.Minimum = 0;
            hScrollBar1.Minimum = 0;

            if (tileSetWidth * tileWidth - splitContainer1.Panel1.Width > 0)
            {
                hScrollBar1.Maximum = tileSetWidth * tileWidth;
            }

            if (vScrollBar1.Visible)
            {
                hScrollBar1.Maximum += vScrollBar1.Width;
            }

            hScrollBar1.LargeChange = 128;
            hScrollBar1.SmallChange = 32;

            hScrollBar1.Maximum += hScrollBar1.LargeChange;

            if (tileSetHeight * tileHeight - splitContainer1.Panel1.Height > 0)
            {
                vScrollBar1.Maximum = tileSetHeight * tileHeight;
            }

            if (hScrollBar1.Visible)
            {
                vScrollBar1.Maximum += hScrollBar1.Height;
            }

            vScrollBar1.LargeChange = 128;
            vScrollBar1.SmallChange = 32;

            vScrollBar1.Maximum += vScrollBar1.LargeChange;
        }


    }
}
